package com.orharlan.carassistant.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.orharlan.carassistant.R;
import com.orharlan.carassistant.datamodel.Datamodel;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SettingsGPSActivity extends Activity implements PropertyChangeListener
{
	public static Datamodel data;
	public static String modeFlag;
	private EditText latiSubMenuEt;
	private EditText longSubMenuEt;
	private TextView submenuTv;
	private TextView latiMenuTv;
	private TextView longMenuTv;
	private TextView titelTv;
	private Button saveNaviBt;
	private Button deleteNaviBt;
	private Button updateNaviBt;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_settingsgps );

		// Call Datamodel
		data = Datamodel.singleton( );

		data.addPropertyChangeListener( this );

		// Receive the Flag for the different modis
		modeFlag = getIntent( ).getStringExtra( "mode" );

		// Set reference to the buttons delete, save and the textview in the xml
		titelTv = (TextView) findViewById( R.id.titelTv );
		submenuTv = (TextView) findViewById( R.id.smallSectionTv );
		latiMenuTv = (TextView) findViewById( R.id.latiMenuTv );
		longMenuTv = (TextView) findViewById( R.id.longMenuTv );
		latiSubMenuEt = (EditText) findViewById( R.id.latiSubMenuEt );
		longSubMenuEt = (EditText) findViewById( R.id.longSubMenuEt );
		saveNaviBt = (Button) findViewById( R.id.saveNaviBt );
		deleteNaviBt = (Button) findViewById( R.id.deleteNaviBt );
		updateNaviBt = (Button) findViewById( R.id.copyIntoEdidTextGpsBt );

		// Set fonts
		Typeface tf = Typeface.createFromAsset( getAssets( ), "fonts/myFont.ttf" );
		titelTv.setTypeface( tf );
		submenuTv.setTypeface( tf );
		latiMenuTv.setTypeface( tf );
		longMenuTv.setTypeface( tf );
		latiSubMenuEt.setTypeface( tf );
		longSubMenuEt.setTypeface( tf );
		saveNaviBt.setTypeface( tf );
		deleteNaviBt.setTypeface( tf );
		updateNaviBt.setTypeface( tf );

		// Set the desired Label for the Submenu
		setDataForInterface( );
		data.getSaveGpsBtListener( ).setContext( this );
		data.getSaveGpsBtListener( ).start( modeFlag ); // Ist schon in onResume
		                                                // drin
	}

	public void setDataForInterface( )
	{
		if( modeFlag.equals( getString( R.string.homeLocation ) ) )
		{
			submenuTv.setText( getString( R.string.homeSettingsTv ) );
			latiSubMenuEt.setText( data.getHomeLatitude( ) + "" );
			longSubMenuEt.setText( data.getHomeLongitude( ) + "" );
		}
		else if( modeFlag.equals( getString( R.string.jobLocation ) ) )
		{
			submenuTv.setText( getString( R.string.jobSettingsTv ) );
			latiSubMenuEt.setText( data.getJobLatitude( ) + "" );
			longSubMenuEt.setText( data.getJobLongitude( ) + "" );
		}
		else if( modeFlag.equals( getString( R.string.carLocation ) ) )
		{
			submenuTv.setText( getString( R.string.carSettingsTv ) );
			latiSubMenuEt.setText( data.getCarLatitude( ) + "" );
			longSubMenuEt.setText( data.getCarLongitude( ) + "" );
		}
	}

	@Override
	protected void onResume( )
	{
		super.onResume( );
		// data.getSaveGpsBtListener( ).start( modeFlag );
	}

	@Override
	public void onBackPressed( )
	{
		super.onBackPressed( );
		data.getSaveGpsBtListener( ).stop( );

		// Write the data in the XML file (Persistence)
		data.getPersXml( ).setDataStorage( );

		Intent intent = new Intent( this, MainActivity.class );
		intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
		startActivity( intent );
	}

	@Override
	protected void onStop( )
	{
		super.onStop( );
		// Write the data in the XML file (Persistence)
		data.getPersXml( ).setDataStorage( );
	}

	@Override
	public boolean onOptionsItemSelected( MenuItem item )
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId( );
		if( id == R.id.action_settings )
		{
			return true;
		}
		return super.onOptionsItemSelected( item );
	}

	// ///////////////////
	// ON Click Listerner

	/**
	 * Listen to the update button in the gps activity. The listener copies the
	 * data from the upper textviews in into the lower editviews for updating
	 * the user data.
	 * 
	 * @param v
	 */
	public void updateGpsBtListener( View v )
	{
		latiSubMenuEt.setText( latiMenuTv.getText( ).toString( ) );
		longSubMenuEt.setText( longMenuTv.getText( ).toString( ) );
	}

	/**
	 * Listen to the save button in the gps activity. Its stores the
	 * 
	 * @param v
	 */
	public void saveGpsBtListener( View v )
	{
		String latiTv;
		String longTv;
		// Check for empty entry in the Editfields, because an empty string can
		// not be parse in double
		if( latiSubMenuEt.getText( ).toString( ).equals( "" ) || longSubMenuEt.getText( ).toString( ).equals( "" ) )
		{
			latiTv = 0.0 + "";
			longTv = 0.0 + "";
		}
		else
		{

			latiTv = latiSubMenuEt.getText( ).toString( );
			longTv = longSubMenuEt.getText( ).toString( );
		}

		// Store the GPS location in the datamodel
		if( SettingsGPSActivity.modeFlag.equals( getString( com.orharlan.carassistant.R.string.carLocation ) ) )
		{
			// In the case: if the User entered his own coordination, it will
			// save the values from the textfield instead of the currentLocation
			// At the initial start all Values will be 0.0, so in this case it
			// should save the current Location
			if( ( latiTv.equals( data.getCarLatitude( ) ) && longTv.equals( data.getCarLongitude( ) ) ) )
			{
				// Saving Current Location, because User haven't modified the
				// coordinations
				data.setCarLocation( data.getCurrentLatitude( ), data.getCurrentLongitude( ) );
				setDataForInterface( );
			}
			else
			{
				// User modified the coodinations, so it saves the user
				// coordinations
				data.setCarLocation( Double.parseDouble( latiTv ), Double.parseDouble( longTv ) );
			}
		}
		else if( SettingsGPSActivity.modeFlag.equals( getString( com.orharlan.carassistant.R.string.homeLocation ) ) )
		{
			if( ( latiTv.equals( data.getHomeLatitude( ) ) && longTv.equals( data.getHomeLongitude( ) ) ) )
			{
				// like above, just for the Home Location
				data.setHomeLocation( data.getCurrentLatitude( ), data.getCurrentLongitude( ) );
				setDataForInterface( );
			}
			else
			{
				// like above, just for the Home Location
				data.setHomeLocation( Double.parseDouble( latiTv ), Double.parseDouble( longTv ) );
			}
		}
		else if( SettingsGPSActivity.modeFlag.equals( getString( com.orharlan.carassistant.R.string.jobLocation ) ) )
		{
			if( ( latiTv.equals( data.getJobLatitude( ) ) && longTv.equals( data.getJobLongitude( ) ) ) )
			{
				// like above, just for the Home Location
				data.setJobLocation( data.getCurrentLatitude( ), data.getCurrentLongitude( ) );
				setDataForInterface( );
			}
			else
			{
				// like above, just for the Home Location
				data.setJobLocation( Double.parseDouble( latiTv ), Double.parseDouble( longTv ) );
			}

		}
	}

	public void deleteGpsBtListener( View v )
	{
		longSubMenuEt.setHint( getString( R.string.longitude ) );
		latiSubMenuEt.setHint( getString( R.string.latitude ) );
		longSubMenuEt.setText( "" );
		latiSubMenuEt.setText( "" );

		// Set the Datamodel to default
		if( modeFlag.equals( getString( R.string.homeLocation ) ) )
		{
			data.setHomeLocation( 0.0, 0.0 );
		}
		else if( modeFlag.equals( getString( R.string.jobLocation ) ) )
		{
			data.setJobLocation( 0.0, 0.0 );
		}
		else if( modeFlag.equals( getString( R.string.carLocation ) ) )
		{
			data.setCarLocation( 0.0, 0.0 );
		}
	}

	// PropertyChanges procedures
	@Override
	public void propertyChange( PropertyChangeEvent arg0 )
	{
		// stops the Location Service
		if( arg0.getPropertyName( ).equals( "carLocationPC" ) )
		{
			// saveGpsBtListener.stop( );
			MainActivity.showShortToast( this, getString( R.string.carSaved ) );
		}
		else if( arg0.getPropertyName( ).equals( "homeLocationPC" ) )
		{
			// saveGpsBtListener.stop( );
			MainActivity.showShortToast( this, getString( R.string.homeSaved ) );
		}
		else if( arg0.getPropertyName( ).equals( "jobLocationPC" ) )
		{
			// saveGpsBtListener.stop( );
			MainActivity.showShortToast( this, getString( R.string.jobSaved ) );
		}
		else if( arg0.getPropertyName( ).equals( "currentLocationPC" ) )
		{
			latiMenuTv.setText( data.getCurrentLatitude( ) + "" );
			longMenuTv.setText( data.getCurrentLongitude( ) + "" );
		}

	}

}
